import fs from 'fs'
import path from 'path'

//导出自动注册路由方法
export const registerRouter = function (router) {
  //获取controller地址
  const filePath = fs.readdirSync('./controller').filter(item => item.endsWith('Controller.js')).map(item => `file:///${path.resolve()}/controller/${item}`)
  //遍历所有的控制器路径
  filePath.forEach(async file => {
    //导入控制器
    let arr = await import(file)
    //遍历控制器
    arr.default.forEach(obj => {
      //根据请求方法注册对应的路由
      if (obj.method === 'get') {
        router.get(obj.url,obj.fn)
      }
      if (obj.method === 'post') {
        router.post(obj.url, obj.fn)
      }
      if (obj.method === 'put') {
        router.put(obj.url, obj.fn)
      }
      if (obj.method === 'delete') {
        router.delete(obj.url, obj.fn)
      }
    })
  
  })
}